#include "npc2bomb.h"
#include "../common.h"

namespace fireplane{

	Npc2Bomb::Npc2Bomb( EffectType type ):
		Effect(type)
	{}

	Npc2Bomb::~Npc2Bomb()
	{}

	void Npc2Bomb::init(CreateCmd* cmd)
	{
		CreateNpc2Bomb* tcmd = (CreateNpc2Bomb*)cmd;
		char buffer[0xff];
		CCPoint pos = tcmd->worldPos;

		pos = Effect::getAllEffectParent()->convertToNodeSpace(pos);

		for(int i = 0 ; i < 4 ; i++) _bomb[i] = NULL;
		for(int i = 0 ; i < 4 ; i++){
			sprintf(buffer,"effect/npc2bomb/%d.png",i);
			_bomb[i] = CCSprite::create(buffer);
			addChild(_bomb[i]);
			this->setPosition(pos);
			this->setScale(0.6f);
		}

		for(int i = 0 ; i < 4 ; i++){
			if( i == 0 ) {
				_bomb[i]->runAction(CCScaleTo::create(1.0f,2.0f));
				_bomb[i]->runAction(
					CCSequence::create(
					CCFadeOut::create(1.0f),
					CCCallFunc::create(this,callfunc_selector(Npc2Bomb::destroyThis)),
					NULL));
			}
			else{	
				_bomb[i]->runAction(CCScaleTo::create(1.0f,6.0f));
				_bomb[i]->runAction(
					CCSequence::create(
					CCFadeOut::create(1.0f),
					CCCallFunc::create(this,callfunc_selector(Npc2Bomb::destroyThis)),
					NULL));
			}
		}

		scheduleUpdate();
	}

	void Npc2Bomb::destroyThis()
	{
		this->removeFromParentAndCleanup(true);
	}

	void Npc2Bomb::update( float delta )
	{
		for(int i = 0 ; i < 4 ; i++){
			if (_bomb[i]->numberOfRunningActions() == 0){
				destroyThis();
			}
		}
	}
}